"""
page object 页面对象 web
面向过程：
"""
import time
import pytest
from selenium import  webdriver
data=[
    {"title": "1.输入正确的用户名和密码,登录成功", "username": "admin", "password": "123456","expect": "s-menu-superadmin"},
    {"title": "2.输入空的用户名和正确的密码密码,登录失败", "username": "", "password": "123456","expect": "登录失败，请检查您的成员名或密码是否填写正确。"},
    {"title": "3.输入正确的用户名和空的密码密码,登录失败", "username": "admin", "password": "","expect": "登录失败，请检查您的成员名或密码是否填写正确。"},
    {"title": "4.输入正确的用户名和空的密码密码,登录失败", "username": "admin", "password": "","expect": "登录失败，请检查您的成员名或密码是否填写正确。"}
        ]
class Basepage:
    def __init__(self,driver=None,index_url=None):
        if index_url != None:#如果这个不为空就用自己的
            self.index_url = index_url
        else:
            self.index_url="http://localhost/ranzhi/www/"#如果为空就传这个网址
        if driver==None:#如果浏览器为空就自己处理浏览器驱动
            self.driver=webdriver.Edge()
            self.driver.maximize_window()
            self.driver.implicitly_wait(10)
        else:
            self.driver=driver
    def coverter(self,target):
        name,value=target.split("=",1)
        return name.strip(),value.strip()
    def locate_element(self,target):
        name,value=self.coverter(target=target)#不一定要写target=target，也可以直接写一个target
        return self.driver.find_element(name,value)
    #1.辅助方法输入方式
    def input(self,target,input_value):
        self.locate_element(target=target).send_keys(input_value)
    #self.locate_element 这个函数返回的是self.driver.find_element，但中间的target=target是代表变成name,value
    #2.辅助方法点击方式
    def type_button(self,target):
        self.locate_element(target=target).click()
    #3.辅助方法获取文本方式
    def get_text(self,target):
        return self.locate_element(target=target).text
    #4.打开首页
    def open_index(self):
        self.driver.get(self.index_url)
    #5.切换框架
    def toframe(self,frame_name):
        self.driver.locate_element(frame_name)
    def get_attr(self,target,attr):
        return self.locate_element(target=target).get_attribute(attr)
class LoginPage(Basepage):
    #//*[@id="account"]
    def __init__(self,driver=None):
        super().__init__(driver=driver)#调用父类的初始化方法
        self.account_loc="xpath=//*[@id='account']"
        self.password_loc="id=password"
        self.submit_loc="id=submit"
        self.assert_text_loc="xpath=/html/body/div[2]/div/div/div[1]/div"
        self.assert_expect="登录失败，请检查您的成员名或密码是否填写正确。"

    def type_account(self,username):
        self.input(self.account_loc,username)
    def type_password(self,pwd):
        self.input(self.password_loc,pwd)
    def type_login_button(self):
        self.type_button(self.submit_loc)
    def get_loginpage_assert_text(self):
        return self.get_text(self.assert_text_loc)

class IndexPage(Basepage):
    def __init__(self,driver=None):
        super().__init__(driver=driver)
        self.menu_superadmin_loc="id=s-menu-superadmin"
        self.assert_text_loc="id=s-menu-superadmin"

    def type_superadmin(self):
        self.type_button(self.menu_superadmin_loc)
        self.toframe("iframe-superadmin")
    def get_indexpage_assert_text(self):
        return self.get_attr(self.assert_text_loc,"id")

class SuperAdminPage(Basepage):
    def __init__(self,driver=None):
        super().__init__(driver=driver)
        self.add_member_loc = "link text=添加成员"
        self.assert_text_loc = "link text=添加成员"
    def type_add_member(self):
        self.type_button(self.add_member_loc)
    def get_superadminpage_assert_text(self):
        return self.get_attr(self.assert_text_loc)

class AddMemberPage(Basepage):
    def __init__(self,driver=None):
        super().__init__(driver=driver)
        self.username_loc = "id=account"
        self.realname_loc="id=realname"
        self.sex_loc=("id=genderf")
        self.dept_loc = "id=dept"
        self.role_loc="xpath=//*[@id='role']/option[3]"
        self.password_loc="id=password1"
        self.password1_loc="id=password2"
        self.email_loc="id=email"
        self.submit_loc="id=submit"

        self.assert_text_loc = "id=submit"
        self.assert_expect = "保存"
    def input_account(self,username):
        self.input(self.username_loc,username)
    def input_realname(self,realname):
        self.input(self.realname_loc,realname)
    def get_superadmin_page_assert_text(self):
        return self.get_text(self.assert_text_loc)

@pytest.mark.parametrize("logindata",data)
def test_login01(logindata):
    driver = webdriver.Edge()
    driver.maximize_window()
    loginpage = LoginPage(driver=driver)
    indexpage = IndexPage(driver=driver)
    # superadminpage = SuperAdminPage(driver=driver)
    # addmemberpage = AddMemberPage(driver=driver)
    # 执行操作
    loginpage.open_index()
    time.sleep(2)
    loginpage.type_account(logindata["username"])
    time.sleep(2)
    loginpage.type_password(logindata["password"])
    time.sleep(2)
    loginpage.type_login_button()
    time.sleep(2)
    if logindata["expect"] == "s-menu-superadmin":
        ele4 = indexpage.get_indexpage_assert_text()

        assert ele4 == logindata["expect"], "登录失败"
    else:
        ele4 = loginpage.get_loginpage_assert_text()
        print(ele4)
        assert ele4 == logindata["expect"], "登录失败"

# if __name__ == '__main__':

